using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using UltimateDefend.Properties;
using System.Threading;

namespace UltimateDefend
{
    public abstract class IAnimation
    {
        #region fields
        protected Bitmap m_bmpDisplay;    //anh hien thi cua animation
        protected Point m_pStart;           //vi tri bat dau cua animation
        protected Point m_pEnd;             //vi tri ket thuc cua animation
        protected Point m_pCurrPoint;       //vi tri hien tai cua animation
        protected int m_iHeight;
        protected int m_iWidth;
        protected ImageAttributes m_ImgAtt;

        protected int m_iNumFrame;            //luu so khung hinh can de hien thi
      
        protected int m_iDistance;          //khoang cach giua hai diem pStart va pEnd
        
        protected Thread m_thread;
        protected ThreadStart m_ts;

        protected int m_iDelay;
        #endregion fields

        #region properties
        public int Delay
        {
            set { this.m_iDelay = value; }
        }
        public ImageAttributes ImgAtt
        {
            get { return this.m_ImgAtt; }
        }
      
        public Bitmap BitmapDisplay
        {
            get { return this.m_bmpDisplay; }
        }
        public int Height
        {
            get { return this.m_iHeight; }
        }
        public int Width
        {
            get { return this.m_iWidth; }
        }
        public Point CurrPoint
        {
            get { return this.m_pCurrPoint; }
        }
        #endregion properties
               
        abstract public void Start();

        public int Distance(Point pStart, Point pEnd)
        {
            int iDistance;
            iDistance = (int)Math.Sqrt((Math.Pow((pStart.X - pEnd.X), 2) + Math.Pow((pStart.Y - pEnd.Y), 2)));
            return iDistance;
        }

    }

    /// <summary>
    /// class de ban
    /// </summary>
    public class ShootOut : IAnimation
    {
        #region methods
        public ShootOut(Point Start, Point End, int idelay)
        {
            //dung tam
            this.m_bmpDisplay = Resources.explosion;
            this.m_iHeight = this.m_bmpDisplay.Height;
            this.m_iWidth = this.m_bmpDisplay.Width / 10;

            this.m_iDelay = idelay;

            //tranparent
            Color c = new Color();
            c = this.m_bmpDisplay.GetPixel(0, 0);
            this.m_ImgAtt = new ImageAttributes();
            this.m_ImgAtt.SetColorKey(c, c);

            //khoi tao diem
            this.m_pStart = Start;
            this.m_pEnd = End;
            this.m_pCurrPoint = this.m_pStart;

            //khoang cach giua hai diem p1 va p2,co the xet truong hop < 0
            this.m_iDistance = Distance(this.m_pStart, this.m_pEnd);

            //tinh toan so frame hinh can hien thi
            if (this.m_iDistance < this.m_iWidth)
            {
                this.m_iNumFrame = 1;
            }
            else
            {
                this.m_iNumFrame = this.m_iDistance / this.m_iWidth;
            }            

            //khoi dong thread
            this.m_ts = new ThreadStart(this.Start);
            this.m_thread = new Thread(this.m_ts);
            this.m_thread.Start();
        }
        public override void Start()
        {
            //tinh vector don vi cua vector di tu pStart->pEnd
            int iDeltaX = (this.m_pEnd.X - this.m_pStart.X);
            int iDeltaY = (this.m_pEnd.Y - this.m_pStart.Y);

            int i;
            for (i = 1; i <= this.m_iNumFrame; i++)
            {
                this.m_pCurrPoint.X = this.m_pStart.X + (this.m_iWidth * iDeltaX * i) / this.m_iDistance;
                this.m_pCurrPoint.Y = this.m_pStart.Y + (this.m_iWidth * iDeltaY * i) / this.m_iDistance;
                Thread.Sleep(100);
            }

            Graphics g = Graphics.FromImage(this.m_bmpDisplay);

            for (i = 1; i < 10; i++)
            {
                g.DrawImage(Resources.explosion, new Rectangle(0, 0, this.Width, this.Height), i * this.Width, 0, this.Width, this.Height, GraphicsUnit.Pixel, this.m_ImgAtt);
                Thread.Sleep(10);
            }

            g.Dispose();
            this.m_pCurrPoint = this.m_pStart;
                       
        }      

        #endregion methods
    }

    /// <summary>
    /// class hien thi 1 vu no
    /// </summary>
    public class Explosion : IAnimation
    {
        #region methods
        public Explosion(Point p)
        {
            //vi tri xay ra vo no
            this.m_pStart = this.m_pEnd = p;

            //cac thuoc tinh ve hinh anh hien thi
            this.m_bmpDisplay = Resources.explosion;
            this.m_iNumFrame = 10;
            this.m_iHeight = Resources.explosion.Height;
            this.m_iWidth = Resources.explosion.Width / this.m_iNumFrame;

            //lay mau de tranparent
            this.m_ImgAtt = new ImageAttributes();
            Color c = this.m_bmpDisplay.GetPixel(0, 0);
            this.m_ImgAtt.SetColorKey(c, c);
        }

        public override void Start()
        {
            throw new Exception("The method or operation is not implemented.");
        }
      
        #endregion methods
    }
}
